package com.ruoyi.maode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.exception.UtilException;
import com.ruoyi.maode.domain.dto.QueryPartBatchDTO;
import com.ruoyi.maode.domain.entity.Part;
import com.ruoyi.maode.domain.entity.PartBatch;
import com.ruoyi.maode.domain.vo.PartBatchDetailVO;
import com.ruoyi.maode.domain.vo.PartBatchVO;
import com.ruoyi.maode.service.PartBatchService;
import com.ruoyi.web.core.config.ResultDTO;
import com.ruoyi.web.util.PageQuery;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 零件批次Controller
 * @author zhangwenqiang
 */

@RestController
@RequestMapping("/partBatch")
public class PartBatchController extends BaseController {

    @Autowired
    private PartBatchService partBatchService;

    @ApiOperation("新增零件批次")
    @PostMapping("/add")
    public ResultDTO add(@RequestBody PartBatch partBatch) {
        return ResultDTO.success(partBatchService.add(partBatch));
    }

    @ApiOperation(value = "批量删除零件批次")
    @DeleteMapping("/deleteByIds")
    public ResultDTO deleteByIds(@RequestBody List<Long> ids) {
        boolean result = partBatchService.removeBatchByIds(ids);
        return result ? ResultDTO.success() : ResultDTO.error();
    }

    @ApiOperation(value = "删除零件批次")
    @DeleteMapping("/deleteById")
    public ResultDTO deleteByIds(@RequestBody Long id) {
        boolean result = partBatchService.removeById(id);
        return result ? ResultDTO.success() : ResultDTO.error();
    }

    @ApiOperation(value = "编辑零件批次")
    @PutMapping("/edit")
    public ResultDTO edit(@RequestBody PartBatch partBatch) {
        // 判断批次号+零件ID是否重复
        PartBatch one = partBatchService.getOne(new LambdaQueryWrapper<PartBatch>()
                .eq(PartBatch::getBatchCode, partBatch.getBatchCode())
                .eq(PartBatch::getPartId,partBatch.getPartId()));
        if (one != null) {
            throw new UtilException("批次已存在，请确认！");
        }
        boolean result = partBatchService.updateById(partBatch);
        return result ? ResultDTO.success("更新成功!") : ResultDTO.error("批次已存在，请确认！");
    }

    @ApiOperation("查询零件批次列表")
    @PostMapping("/list")
    public ResultDTO<Page<PartBatchVO>> getPage(@RequestBody PageQuery<QueryPartBatchDTO> dto) {
        Page<PartBatchVO> supplierList = partBatchService.selectPartBatchList(dto);
        return ResultDTO.success(supplierList);
    }

    @ApiOperation("获取批次详情")
    @GetMapping("/detail/{id}")
    public ResultDTO<PartBatchDetailVO> getDetail(@PathVariable("id")Long id) {
        return ResultDTO.success(partBatchService.getDetail(id));
    }


}
